import { Controller } from "../../mvc/core/Controller";
import { EBattlefieldPlayerState } from "../model/defin/EBattlefieldPlayerState";
import { NewBattlefieldMiniScoreView } from "../view/battlefieldNew/NewBattlefieldMiniScoreView";
import { IView } from "../../mvc/interfaces/IView";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { Alert } from "../../../com/mui/controls/Alert";
import { Language } from "../../../extend/language/Language";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { MsgManager } from "../manager/MsgManager";
import { GameProxy } from "../mvc/GameProxy";
import { INewBFEntranceModule } from "../../../modules/interfaces/INewBFEntranceModule";
import { NewBFEntranceModule } from "../../../modules/NewBFEntranceModule";
import { INewBFTopMudule } from "../../../modules/interfaces/INewBFTopMudule";
import { NewBFTopModule } from "../../../modules/NewBFTopModule";
import { NewBattlefiledMyResultWindow } from "../view/battlefieldNew/NewBattlefiledMyResultWindow";
import { NewBattlefieldSparkView } from "../view/battlefieldNew/NewBattlefieldSparkView";
import { SNewBattleFieldOpen } from "../../../Message/Public/SNewBattleFieldOpen";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { SystemSetter } from "../view/system/SystemSetter";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
import { SNewBattleFieldMyResult } from "../../../Message/Public/SNewBattleFieldMyResult";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { SNewBattleFieldInspireInfo } from "../../../Message/Public/SNewBattleFieldInspireInfo";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { ClockManager } from "../manager/ClockManager";
import { NewBattlefieldIconView } from "../view/battlefieldNew/NewBattlefieldIconView";
type int = number;
//class NewBattlefieldController
    
    export  class NewBattlefieldController extends Controller
    {
        constructor()
        {
            
            super();this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusClose;
            return;
        }

        public get newBattlefieldMiniScoreView(): NewBattlefieldMiniScoreView
        {
            if (!this._newBattlefieldMiniScoreView) 
            {
                this._newBattlefieldMiniScoreView = new NewBattlefieldMiniScoreView();
            }
            return this._newBattlefieldMiniScoreView;
        }

        protected /* override */ initView(): IView
        {
            return super.initView();
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.NewBattlefieldOpen, this.onBattlefieldOpen);
            NetDispatcher.addCmdListener(ServerCommand.NewBattlefieldClose, this.onBattlefieldClose);
            NetDispatcher.addCmdListener(ServerCommand.NewBattlefieldListUpdate, this.onBattlefieldList);
            NetDispatcher.addCmdListener(ServerCommand.NewBattlefieldPlayerScoresUpdate, this.onBattlefieldPlayerScoresUpdate);
            NetDispatcher.addCmdListener(ServerCommand.NewBattlefieldPlayerCreditUpdate, this.onBattlefieldPlayerCreditUpdate);
            NetDispatcher.addCmdListener(ServerCommand.NewBattlefieldFroceCreditUpdate, this.onBattlefieldForceCreditUpdate);
            NetDispatcher.addCmdListener(ServerCommand.NewBattlefieldMyResultUpdate, this.onMyResultUpdate);
            NetDispatcher.addCmdListener(ServerCommand.RoleFightAttributeChange, this.onRoleAttributeChange);
            NetDispatcher.addCmdListener(ServerCommand.ActiveAddInterHonour, this.onActiveAddInterHonourHandler);
            NetDispatcher.addCmdListener(ServerCommand.NewBattlefieldPersonalInspireInfoUpdate, this.onPersonalInspireInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.NewBattlefieldAllInspireInfoUpdate, this.onAllInspireInfoUpdate);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            Dispatcher.addEventListener(EventName.NewBattlefieldShowIntroduce, this.onBattlefieldShowIntroduce);
            Dispatcher.addEventListener(EventName.NewBattlefieldRequestList, this.onBattlefieldRequestList);
            Dispatcher.addEventListener(EventName.NewBattlefieldRequestComein, this.onBattlefieldRequestComein);
            Dispatcher.addEventListener(EventName.NewBattlefieldRequestResult, this.onBattlefieldRequestResult);
            Dispatcher.addEventListener(EventName.NewBattlefieldRequestLeaveAffirm, this.onBattlefieldRequestLeaveAffirm);
            Dispatcher.addEventListener(EventName.SysSettingSuccess, this.onSettingChange);
            Dispatcher.addEventListener(EventName.NewBattlefieldRequestLeave, this.onRequestLeave);
            Dispatcher.addEventListener(EventName.NewBattlefieldShowOrHideEntrance, this.onShowOrHideEntrace);
            Dispatcher.addEventListener(EventName.NewBattlefieldAutoShowOrHideEntrance, this.onAutoShowOrHideEntrace);
            Dispatcher.addEventListener(EventName.NewBattlefieldShowOrHideTop, this.onShowOrHideTop);
            Dispatcher.addEventListener(EventName.NewBattlefieldAutoShowOrHideTop, this.onAutoShowOrHideTop);
            Dispatcher.addEventListener(EventName.NewBattlefieldWorship, this.onWorship);
            Dispatcher.addEventListener(EventName.StageResize, this.onStageResize);
            return;
        }

        /* internal  */onRequestLeave(arg1: DataEvent): void
        {
            Alert.show(Language.getString(41635), null, Alert.OK | Alert.CANCEL, null, this.onClose);
            return;
        }

        /* internal  */onClose(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.NewBattlefieldRequestLeaveAffirm));
            }
            return;
        }

        /* internal  */onShowOrHideEntrace(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            if (loc1) 
            {
                this.newBFEntranceModule.show();
            }
            else if (this._newBFEntranceModule) 
            {
                this.newBFEntranceModule.hide();
            }
            return;
        }

        /* internal  */onAutoShowOrHideEntrace(arg1: DataEvent): void
        {
            if (this.newBFEntranceModule.isHide) 
            {
                if (cache.role.entityInfo.level >= LevelLimitDefin.BattlefieldMinLevel) 
                {
                    this.newBFEntranceModule.show();
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getStringByParam(41628, LevelLimitDefin.BattlefieldMinLevel.toString()));
                }
            }
            else if (this._newBFEntranceModule) 
            {
                this.newBFEntranceModule.hide();
            }
            return;
        }

        /* internal  */onShowOrHideTop(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            if (loc1) 
            {
                this.newBFTopModule.show();
            }
            else if (this._newBFTopModule) 
            {
                this.newBFTopModule.hide();
            }
            return;
        }

        /* internal  */onAutoShowOrHideTop(arg1: DataEvent): void
        {
            if (this.newBFTopModule.isHide) 
            {
                this.newBFTopModule.show();
                Dispatcher.dispatchEvent(new DataEvent(EventName.NewBattlefieldRequestResult));
            }
            else if (this._newBFTopModule) 
            {
                this.newBFTopModule.hide();
            }
            return;
        }

        /* internal  */onWorship(arg1: DataEvent): void
        {
            GameProxy.newBattlefield.inspire(int(arg1.data));
            return;
        }

        /* internal  */onStageResize(arg1: DataEvent): void
        {
            if (this._newBattlefieldSparkView) 
            {
                this._newBattlefieldSparkView.resetPosition();
            }
            return;
        }

        public get newBFEntranceModule(): INewBFEntranceModule
        {
            if (!this._newBFEntranceModule) 
            {
                this._newBFEntranceModule = new NewBFEntranceModule();
                if (!this._battlefieldOpen) 
                {
                };
            }
            return this._newBFEntranceModule;
        }

        public get newBFTopModule(): INewBFTopMudule
        {
            if (!this._newBFTopModule) 
            {
                this._newBFTopModule = new NewBFTopModule();
            }
            return this._newBFTopModule;
        }

        public get myResultWindow(): NewBattlefiledMyResultWindow
        {
            if (!this._myResultWindow) 
            {
                this._myResultWindow = new NewBattlefiledMyResultWindow();
            }
            return this._myResultWindow;
        }

        public get newBattlefieldSparkView(): NewBattlefieldSparkView
        {
            if (!this._newBattlefieldSparkView) 
            {
                this._newBattlefieldSparkView = new NewBattlefieldSparkView();
            }
            return this._newBattlefieldSparkView;
        }

        public get isInBattlefield(): Boolean
        {
            return this._isInBattlefield;
        }

        /* internal  */onBattlefieldOpen(arg1: SNewBattleFieldOpen): void
        {
            this._isCloseBattleField = false;
            this._battlefieldOpen = arg1;
            if (this._isInBattlefield) 
            {
                this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusOpen;
            }
            else 
            {
                this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusWait;
            }
            this._isAtStartTime = false;
            var loc1=false;
            this.updateViewState();
            if (!this._isAtStartTime && cache.role.entityInfo.level >= LevelLimitDefin.BattlefieldMinLevel) 
            {
                if (!GameMapUtil.isNewBattlefield()) 
                {
                    this.newBFEntranceModule.show();
                }
            }
            if (!loc1 && cache.role.entityInfo.level >= LevelLimitDefin.BattlefieldMinLevel) 
            {
                this.newBattlefieldIconView.showLightMask();
            }
            else 
            {
                this.newBattlefieldIconView.hideLightMask();
            }
            if (loc1) 
            {
                this.newBattlefieldIconView.startCountdownLimitEnter(this._battlefieldOpen.openDt);
            }
            return;
        }

        public set isInBattlefield(arg1: Boolean)
        {
            this._isInBattlefield = arg1;
            return;
        }

        /* internal  */onBattlefieldClose(arg1: Object): void
        {
            this._isCloseBattleField = true;
            if (!this._isInBattlefield) 
            {
                this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusClose;
                this.updateViewState();
            }
            this.newBFTopModule.showLeaveBtn();
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (!this._isInBattlefield && GameMapUtil.isNewBattlefield()) 
            {
                this._isInBattlefield = true;
                this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusOpen;
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyEnterCopyMap));
                this.updateViewState();
                this.newBattlefieldSparkView.show();
                if (this._battlefieldOpen) 
                {
                    this.battlefieldCountDownUpdate(this._battlefieldOpen.endDt);
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
                this.lastSettingHideLifeMagic = SystemSetter.currentSetter.hideLifeMagic;
                SystemSetter.currentSetter.hideLifeMagic = false;
                ThingUtil.entityUtil.updateLifeShow();
                Dispatcher.dispatchEvent(new DataEvent(EventName.NewBattlefieldEnterMap));
            }
            if (this._isInBattlefield && !GameMapUtil.isNewBattlefield()) 
            {
                this._isInBattlefield = false;
                if (this._isCloseBattleField) 
                {
                    this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusClose;
                }
                else 
                {
                    this._currentBFState = EBattlefieldPlayerState.EBattlefieldStatusWait;
                    this.onBattlefieldRequestList(null);
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveCopyMap));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, true));
                this.updateViewState();
                this.newBattlefieldSparkView.hide();
                Dispatcher.dispatchEvent(new DataEvent(EventName.NewBattlefieldLeaveMap));
                if (this.lastSettingHideLifeMagic && !this.isChangeAtMiddle) 
                {
                    SystemSetter.currentSetter.hideLifeMagic = true;
                    ThingUtil.entityUtil.updateLifeShow();
                }
            }
            return;
        }

        /* internal  */onSettingChange(arg1: DataEvent): void
        {
            this.isChangeAtMiddle = true;
            return;
        }

        /* internal  */battlefieldCountDownUpdate(arg1: Date): void
        {
            this.newBattlefieldMiniScoreView.startCountdown(arg1);
            return;
        }

        /* internal  */onBattlefieldList(arg1: Array<any>): void
        {
            var loc2=null;
            this.newBFEntranceModule.updateNewBattlefieldList(arg1);
            var loc1=0;
            while (loc1 < arg1.length) 
            {
                loc2 = arg1[loc1];
                if (loc2.belongTo && loc2.close) 
                {
                    this.newBattlefieldIconView.hideLightMask();
                    break;
                }
                ++loc1;
            }
            return;
        }

        /* internal  */onBattlefieldPlayerScoresUpdate(arg1: Object): void
        {
            this.newBFTopModule.refreshScores(cache.newBattlefield.vcNewBattlePlayerScore);
            return;
        }

        /* internal  */onBattlefieldPlayerCreditUpdate(arg1: Object): void
        {
            this.newBattlefieldMiniScoreView.updateMyScore(cache.newBattlefield.myScore);
            return;
        }

        /* internal  */onBattlefieldForceCreditUpdate(arg1: Object): void
        {
            this.newBattlefieldMiniScoreView.updateCampScore(cache.newBattlefield.forceScore1, cache.newBattlefield.forceScore2);
            if (cache.newBattlefield.leftTime > 0) 
            {
                this.newBattlefieldMiniScoreView.updateLeftTime(cache.newBattlefield.leftTime);
            }
            return;
        }

        /* internal  */onMyResultUpdate(arg1: SNewBattleFieldMyResult): void
        {
            this.myResultWindow.show();
            this.myResultWindow.updateResult(arg1);
            return;
        }

        /* internal  */onRoleAttributeChange(arg1: Object): void
        {
            if (this._newBattlefieldSparkView) 
            {
                this._newBattlefieldSparkView.updateTipTxt();
            }
            return;
        }

        /* internal  */onActiveAddInterHonourHandler(arg1: SActiveToClient): void
        {
            if (this._newBattlefieldIconView && !this._newBattlefieldIconView.isHide) 
            {
                this._newBattlefieldIconView.updateActiveToClientInfo(arg1);
            }
            return;
        }

        /* internal  */onPersonalInspireInfoUpdate(arg1: SNewBattleFieldInspireInfo): void
        {
            if (this._newBattlefieldSparkView) 
            {
                this._newBattlefieldSparkView.updatePersonalInspire(arg1);
            }
            return;
        }

        /* internal  */onAllInspireInfoUpdate(arg1: SNewBattleFieldInspireInfo): void
        {
            if (this._newBattlefieldSparkView) 
            {
                this._newBattlefieldSparkView.updateTotalInspire(arg1);
            }
            return;
        }

        /* internal  */onBattlefieldShowIntroduce(arg1: DataEvent): void
        {
            this.newBFEntranceModule.show();
            return;
        }

        /* internal  */onBattlefieldRequestList(arg1: DataEvent): void
        {
            GameProxy.newBattlefield.getNewBattleFieldList();
            return;
        }

        /* internal  */onBattlefieldRequestComein(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            GameProxy.newBattlefield.enterNewBattleField(loc1);
            return;
        }

        /* internal  */onBattlefieldRequestResult(arg1: DataEvent): void
        {
            GameProxy.newBattlefield.getBattleFieldScoreList();
            return;
        }

        /* internal  */onBattlefieldRequestLeaveAffirm(arg1: DataEvent): void
        {
            GameProxy.newBattlefield.leftBattleField();
            return;
        }

        /* internal  */updateViewState(): void
        {
            this.newBattlefieldIconView.state = this._currentBFState;
            var loc1=this._currentBFState;
            switch (loc1) 
            {
                case EBattlefieldPlayerState.EBattlefieldStatusClose:
                {
                    if (this._newBattlefieldIconView) 
                    {
                        this.newBattlefieldIconView.hide();
                    }
                    if (this._newBattlefieldMiniScoreView) 
                    {
                        this.newBattlefieldMiniScoreView.hide();
                        this.newBattlefieldMiniScoreView.stopCountDown();
                    }
                    if (this._myResultWindow) 
                    {
                        this._myResultWindow.hide();
                    }
                    break;
                }
                case EBattlefieldPlayerState.EBattlefieldStatusOpen:
                {
                    if (this._newBattlefieldIconView) 
                    {
                        this.newBattlefieldIconView.hide();
                        this.newBattlefieldIconView.stopLimitEnterCountDown();
                    }
                    this.newBattlefieldMiniScoreView.show();
                    if (this._battlefieldOpen) 
                    {
                        this.battlefieldCountDownUpdate(this._battlefieldOpen.endDt);
                    }
                    break;
                }
                case EBattlefieldPlayerState.EBattlefieldStatusWait:
                {
                    this.newBattlefieldIconView.show();
                    if (this._newBattlefieldMiniScoreView) 
                    {
                        this._newBattlefieldMiniScoreView.hide();
                    }
                    if (this._myResultWindow) 
                    {
                        this._myResultWindow.hide();
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */isInBattlefieldTime(): Boolean
        {
            if (!this._battlefieldOpen) 
            {
                return false;
            }
            if (ClockManager.instance.nowDate.time > this._battlefieldOpen.openDt.time && ClockManager.instance.nowDate.time < this._battlefieldOpen.endDt.time) 
            {
                return true;
            }
            return false;
        }

        public get newBattlefieldIconView(): NewBattlefieldIconView
        {
            if (!this._newBattlefieldIconView) 
            {
                this._newBattlefieldIconView = new NewBattlefieldIconView();
            }
            return this._newBattlefieldIconView;
        }

        private /* var */_currentBFState: EBattlefieldPlayerState;

        private /* var */_battlefieldOpen: SNewBattleFieldOpen;

        private /* var */_isAtStartTime: Boolean=false;

        private /* var */_isInBattlefield: Boolean=false;

        private /* var */lastSettingHideLifeMagic: Boolean=false;

        private /* var */isChangeAtMiddle: Boolean=false;

        private /* var */_isCloseBattleField: Boolean=true;

        private /* var */_myResultWindow: NewBattlefiledMyResultWindow;

        private /* var */_newBFEntranceModule: INewBFEntranceModule;

        private /* var */_newBattlefieldSparkView: NewBattlefieldSparkView;

        private /* var */_newBattlefieldMiniScoreView: NewBattlefieldMiniScoreView;

        private /* var */_newBFTopModule: INewBFTopMudule;

        private /* var */_newBattlefieldIconView: NewBattlefieldIconView;
    }
